import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

const Home_URL = "https://m.ctrip.com/restapi/soa2/16189/json/appSuggest";

class SearchDao {
  static Future<dynamic> fetch(keyword) async {
    try {
      dynamic response = await http.post(Home_URL, body: {"keyword": keyword});
      Utf8Decoder utf8decoder = Utf8Decoder(); //fix中文乱码
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      List k = [];
      result["result"].forEach((dynamic item) {
        item["items"].forEach((i) {
          k.add(i);
        });
      });
      return {"keyword": keyword, "k": k};
    } catch (e) {
      print(e);
      throw Exception("Failed to load search.json");
    }
  }
}
